package com.pet.backend.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.pet.backend.pojo.Contact;
import com.pet.backend.pojo.Message;
import com.pet.backend.pojo.Vo;
import com.pet.backend.service.impl.MessageServiceImpl;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 *  留言
 * </p>
 *
 * @author pet
 * @since 2024-07-13
 */
@CrossOrigin
@RestController
@RequestMapping("/message")
public class MessageController {

    @Resource
    private MessageServiceImpl messageService;

    // 搜索
    @GetMapping("/search/{title}/{current}")
    public Vo<Message> search(@PathVariable("title") String title, @PathVariable("current") Integer current){
        if (title.equals("")){
            return new Vo<>();
        }
        LambdaQueryWrapper<Message> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(Message::getTitle,title);
        Page<Message> messagePage = new Page<>(current <= 0 ? 1 : current,6);
        IPage<Message> page = messageService.page(messagePage, wrapper);
        if (page.getTotal() == 0){
            // 表示没有数据
            return new Vo<>();
        }
        List<Message> messages = page.getRecords();
        // 总条数
        long total = page.getTotal();
        // 当前页
        long pageCurrent = page.getCurrent();
        // 返回
        return new Vo<>(true,messages,pageCurrent,total);
    }
    // 分页查询
    @GetMapping("/page/{current}")
    public Vo<Message> page(@PathVariable("current") Integer current) {
        Page<Message> messagePage = new Page<>(current >= 0 ? current : 1, 6);
        IPage<Message> page = messageService.page(messagePage);
        if (page.getTotal() == 0) {
            // 表示没有数据直接返回
            return new Vo<>(false, "目前没有留言！");
        }
        // 数据
        List<Message> messages = page.getRecords();
        // 当前页
        long curr = page.getCurrent();
        // 总记录数
        long total = page.getTotal();
        // 返回
        return new Vo<>(true, messages, curr, total);
    }
    // 修改状态
    @GetMapping("/updateStu/{id}")
    public Vo<Message> updateStu(@PathVariable("id") String id){
        Message message = new Message();
        message.setId(id);
        message.setStatus("已读");
        boolean b = messageService.updateById(message);
        if (b) {
            return new Vo<>(b,"确定已读！");
        }else {
            return new Vo<>(b, "修改状态失败，请稍后重试！");
        }
    }
    // 删除
    @GetMapping("/delete/{id}")
    public Vo<Message> delete(@PathVariable("id") String id){
        boolean b = messageService.removeById(id);
        if (b){
            return new Vo<>(b,"删除成功！");
        }else {
            return new Vo<>(b,"删除失败，请稍后重试！");
        }
    }

    // 新增
    @PostMapping("/insert")
    public Vo<Message> insert(@RequestBody Message message){
        message.setStatus("未读");
        boolean b = messageService.save(message);
        if (b){
            return new Vo<>(b,"留言成功！");
        }else {
            return new Vo<>(b,"留言失败，请稍后重试");
        }
    }
}
